﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.util {
    import Engine.Managers.*;
    import flash.geom.*;
    import Display.*;
    import Modules.stats.experiments.*;
    import Engine.*;
    import flash.external.*;

    public class FrameManager {

        private static const METHOD_NAVIGATE_TO:String = "ZYFrameManager.navigateTo";
        private static const METHOD_TRAY_POPULATE:String = "ZYFrameManager.dynamicTrayPopulate";
        private static const METHOD_TRAY_PRELOAD:String = "ZYFrameManager.dynamicTrayPreload";
        private static const METHOD_TRAY_CLOSE:String = "ZYFrameManager.dynamicTrayClose";

        private static var silentLoadCallback:Function;
        private static var silentLoadDlg:FrameLoaderDialog;
        private static var loadInProgress:Boolean;
        private static var lastUrl:String;
        private static var dynamicTrayOpen:Boolean;
        private static var initTrayPreloads:Array;

        public static function navigateTo(_arg1:String, _arg2:String=null, _arg3:Function=null, _arg4:Point=null, _arg5:Boolean=true):void{
            var _local6:Boolean = showFrame(_arg1, METHOD_NAVIGATE_TO, _arg2, _arg3, _arg4, _arg5);
        }
        public static function showTray(_arg1:String, _arg2:String=null, _arg3:Function=null, _arg4:Point=null, _arg5:Boolean=false):void{
            var _local6:Boolean;
            if (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_CREW_POPUP_MFS) == ExperimentDefinitions.TRAY_POPUP_MFS){
                _local6 = showFrame(_arg1, METHOD_TRAY_POPULATE, _arg2, _arg3, _arg4, _arg5);
            } else {
                navigateTo(_arg1, _arg2, _arg3, _arg4, true);
            };
            if (_local6){
                dynamicTrayOpen = true;
            };
        }
        public static function preloadTray(_arg1:String, _arg2:Boolean):void{
            var url:* = _arg1;
            var forceReload:* = _arg2;
            if (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_CREW_POPUP_MFS) == ExperimentDefinitions.TRAY_POPUP_MFS){
                try {
                    if (ExternalInterface.available){
                        ExternalInterface.call(METHOD_TRAY_PRELOAD, url, forceReload);
                    };
                } catch(e:Error) {
                    ErrorManager.addError(ZLoc.t("Dialogs", "NavigateToFailed", {url:url}));
                };
            };
        }
        public static function navigateImmediatelyTo(_arg1:String):void{
            var url:* = _arg1;
            if (loadInProgress){
                return;
            };
            try {
                if (ExternalInterface.available){
                    if (Utilities.isFullScreen()){
                        Utilities.setFullScreen(false);
                    };
                    ExternalInterface.call(METHOD_NAVIGATE_TO, url);
                    lastUrl = url;
                };
            } catch(e:Error) {
                ErrorManager.addError(ZLoc.t("Dialogs", "NavigateToFailed", {url:url}));
            };
        }
        public static function frameLoadComplete(_arg1:Boolean=true):void{
            loadInProgress = false;
            if (silentLoadDlg != null){
                silentLoadDlg.close();
                silentLoadDlg = null;
            };
            if (silentLoadCallback != null){
                silentLoadCallback(_arg1);
                silentLoadCallback = null;
            };
        }
        private static function showFrame(_arg1:String, _arg2:String, _arg3:String=null, _arg4:Function=null, _arg5:Point=null, _arg6:Boolean=true):Boolean{
            var success:* = false;
            var url:* = _arg1;
            var method:* = _arg2;
            var loadingText = _arg3;
            var completeCallback = _arg4;
            var targetPos = _arg5;
            var silentLoad:Boolean = _arg6;
            if (loadInProgress){
                return (false);
            };
            if (dynamicTrayOpen){
                closeTray();
            };
            try {
                if (ExternalInterface.available){
                    silentLoadCallback = completeCallback;
                    if (!loadingText){
                        loadingText = ZLoc.t("Dialogs", "FrameLoading");
                    };
                    silentLoadDlg = new FrameLoaderDialog(loadingText, targetPos);
                    UI.displayPopup(silentLoadDlg, false, "FrameLoading");
                    loadInProgress = true;
                    if (Utilities.isFullScreen()){
                        Utilities.setFullScreen(false);
                    };
                    success = ExternalInterface.call(method, url, silentLoad);
                    if (!success){
                        frameLoadComplete(false);
                    };
                    lastUrl = url;
                    return (success);
                };
            } catch(e:Error) {
                ErrorManager.addError(ZLoc.t("Dialogs", "NavigateToFailed", {url:url}));
            };
            return (false);
        }
        public static function doInitPreloads():void{
            var _local1:XMLList;
            var _local2:XML;
            var _local3:String;
            dynamicTrayOpen = false;
            if (!Global.player.isNewPlayer){
                _local1 = Global.gameSettings.getInitTrayPreloads();
                initTrayPreloads = [];
                for each (_local2 in _local1) {
                    _local3 = _local2.@url;
                    preloadTray(_local3, false);
                    initTrayPreloads.push(_local3);
                };
            };
        }
        public static function onDynamicTrayClosed():void{
            var _local1:String;
            dynamicTrayOpen = false;
            for each (_local1 in initTrayPreloads) {
                if (_local1 == lastUrl){
                    preloadTray(_local1, true);
                    break;
                };
            };
        }
        public static function getLastUrl():String{
            return (lastUrl);
        }
        public static function closeTray():void{
            frameLoadComplete();
            try {
                if (ExternalInterface.available){
                    ExternalInterface.call(METHOD_TRAY_CLOSE);
                };
            } catch(e:Error) {
                ErrorManager.addError(ZLoc.t("Dialogs", "CloseTrayFailed", {url:lastUrl}));
            };
            onDynamicTrayClosed();
        }

    }
}//package Classes.util 
